package com.yihu.wlyy.job;

import com.yihu.wlyy.entity.*;
import com.yihu.wlyy.entity.address.Hospital;
import com.yihu.wlyy.entity.address.Town;
import com.yihu.wlyy.entity.consult.ConsultTeam;
import com.yihu.wlyy.entity.doctor.Doctor;
import com.yihu.wlyy.repository.*;
import com.yihu.wlyy.web.quota.WlyyJobConfigVO;
import com.yihu.wlyy.web.quota.WlyyQuotaVO;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 咨询模块的指标执行类
 */
@Component
public class ConsultJob implements Job {

    private WlyyQuotaVO wlyyQuota;//指标对象
    private WlyyJobConfigVO wlyyJobConfig;//配置对象
    @Autowired
    private WlyyQuotaResultDao wlyyQuotaResultDao;//指标结果Dao
    @Autowired
    private WlyyJobLogDao wlyyJobLogDao;//执行日志Dao
    @Autowired
    private DoctorDao doctorDao;
    @Autowired
    private HospitalDao hospitalDao;
    @Autowired
    private TownDao townDao;
    @Autowired
    private ConsultTeamDao consultTeamDao;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    String yesterday;
    String now;

    @Override
    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
        //初始化参数
        JobDataMap map = context.getJobDetail().getJobDataMap();
        wlyyQuota = (WlyyQuotaVO) map.get("quota");
        wlyyJobConfig = (WlyyJobConfigVO) map.get("jobConfig");

        now= StringUtils.isEmpty(map.get("now"))?SignJob.getDayString(0):map.get("now").toString();
        yesterday= StringUtils.isEmpty(map.get("yesterday"))?SignJob.getDayString(-1):map.get("yesterday").toString();  //線刪除今天的数据
        //计算指标
        computequota();
    }
    @Transactional
    private void computequota() {
        jdbcTemplate.execute("delete from wlyy_quota_result where quota_date='"+yesterday+"' and quato_code='"+3+"'");

        //新建任务日志对象
        WlyyJobLog wlyyJobLog = new WlyyJobLog();
        wlyyJobLog.setJobStartTime(new Date());
        wlyyJobLog.setJobId(wlyyJobConfig.getId());
        wlyyJobLog.setJobName(wlyyJobConfig.getJobName());

        //查找出系统全部的健康医生
        List<Doctor> doctors = doctorDao.findAllZKDoctot();
        Map<String, Doctor> doctorsMap = new HashMap<String, Doctor>();
        for (Doctor doctor : doctors) {
            doctorsMap.put(doctor.getCode(), doctor);
        }
        //查找出系统全部的机构
        List<Hospital> hospitals = hospitalDao.findHospital2();
        Map<String, Hospital> hospitalsMap = new HashMap<String, Hospital>();
        for (Hospital hospital : hospitals) {
            hospitalsMap.put(hospital.getCode(), hospital);
        }
        //查找出厦门市全部的区
        List<Town> towns = townDao.findByCityCode(Constant.city);
        Map<String, Town> townsMap = new HashMap<String, Town>();
        for (Town town : towns) {
            townsMap.put(town.getCode(), town);
        }
        //找出今天的咨询信息
       List<ConsultTeam> consultTeams = consultTeamDao.findByCzrqyYesterday(yesterday,now);

        Map<String, Long> tjTownMap = new HashMap<String, Long>();//区级的统计map key 是区行政区划350200
        Map<String, Long> tjOrgMap = new HashMap<String, Long>();//机构的统计map key 是机构的code
        Map<String, Long> tjQkdoctorMap = new HashMap<String, Long>();//团队级的统计map 目前没有团队 先用全科医生统一  key doctorCode
        Long cityCount = 0L;
        //统计有咨询的
        for (ConsultTeam consultTeam : consultTeams) {
            String doctorCode = consultTeam.getDoctor();//得到咨询中健康医生的code
            //统计团队
            if (tjQkdoctorMap.containsKey(doctorCode)) {
                tjQkdoctorMap.put(doctorCode, tjQkdoctorMap.get(doctorCode) + 1);
            } else {
                tjQkdoctorMap.put(doctorCode, 1L);
            }
            //判断医生属于哪个机构
            Doctor doctor = doctorsMap.get(doctorCode);
            if(doctor==null){
                continue;
            }
            String orgCode = doctor.getHospital();
            //统计机构
            if (tjOrgMap.containsKey(orgCode)) {
                tjOrgMap.put(orgCode, tjOrgMap.get(orgCode) + 1);
            } else {
                tjOrgMap.put(orgCode, 1L);
            }
            String townCode = doctor.getTown();
            //统计区
            if (tjTownMap.containsKey(townCode)) {
                tjTownMap.put(townCode, tjTownMap.get(townCode) + 1);
            } else {
                tjTownMap.put(townCode, 1L);
            }
            //统计市
            cityCount++;
        }
        //保存统计的结果
        //保存全科医生的咨询统计
        for (Map.Entry<String, Doctor> entry : doctorsMap.entrySet()) {
            Doctor doctor = doctorsMap.get(entry.getKey());//得到健康管理师
            WlyyQuotaResult wlyyQuotaResult = new WlyyQuotaResult();
            wlyyQuotaResult.setDel("1");
            wlyyQuotaResult.setOrgCode(doctor.getHospital());
            wlyyQuotaResult.setOrgName(doctor.getHosptialName());
            wlyyQuotaResult.setCity(doctor.getCity());
            wlyyQuotaResult.setCityName(doctor.getCityName());
            wlyyQuotaResult.setQuatoCode(wlyyQuota.getId());
            wlyyQuotaResult.setQuatoName(wlyyQuota.getName());
            wlyyQuotaResult.setQuotaDate(getYesterday());
            wlyyQuotaResult.setQkdoctorJobName(doctor.getJobName());
            wlyyQuotaResult.setQkdoctorName(doctor.getName());
            wlyyQuotaResult.setQkdoctorCode(doctor.getCode());
            wlyyQuotaResult.setTown(doctor.getTown());
            wlyyQuotaResult.setTownName(doctor.getTownName());
            wlyyQuotaResult.setLevel1Type("1");
            wlyyQuotaResult.setCreateTime(new Date());
            //判断全科医生是否有咨询量
            if (tjQkdoctorMap.containsKey(doctor.getCode())) {
                wlyyQuotaResult.setResult(tjQkdoctorMap.get(doctor.getCode()) + "");
            } else {
                wlyyQuotaResult.setResult("0");
            }
            wlyyQuotaResultDao.save(wlyyQuotaResult);
        }
        //保存机构的咨询统计
        for (Map.Entry<String, Hospital> entry : hospitalsMap.entrySet()) {
            Hospital hospital = hospitalsMap.get(entry.getKey());
            WlyyQuotaResult wlyyQuotaResult = new WlyyQuotaResult();
            wlyyQuotaResult.setDel("1");
            wlyyQuotaResult.setOrgCode(hospital.getCode());
            wlyyQuotaResult.setOrgName(hospital.getName());
            wlyyQuotaResult.setCity(hospital.getCity());
            wlyyQuotaResult.setCityName(hospital.getCityName());
            wlyyQuotaResult.setTown(hospital.getTown());
            wlyyQuotaResult.setTownName(hospital.getTownName());
            wlyyQuotaResult.setQuatoCode(wlyyQuota.getId());
            wlyyQuotaResult.setQuatoName(wlyyQuota.getName());
            wlyyQuotaResult.setQuotaDate(getYesterday());
            wlyyQuotaResult.setCreateTime(new Date());
            wlyyQuotaResult.setLevel1Type("2");
            //判断全科医生是否有咨询量
            if (tjOrgMap.containsKey(hospital.getCode())) {
                wlyyQuotaResult.setResult(tjOrgMap.get(hospital.getCode()) + "");
            } else {
                wlyyQuotaResult.setResult("0");
            }
            wlyyQuotaResultDao.save(wlyyQuotaResult);
        }
        //保存区级的咨询统计
        for (Map.Entry<String, Town> entry : townsMap.entrySet()) {
            Town town = townsMap.get(entry.getKey());
            WlyyQuotaResult wlyyQuotaResult = new WlyyQuotaResult();
            wlyyQuotaResult.setDel("1");
            wlyyQuotaResult.setCity(town.getCity());
            wlyyQuotaResult.setCityName("厦门市");
            wlyyQuotaResult.setTown(town.getCode());
            wlyyQuotaResult.setTownName(town.getName());
            wlyyQuotaResult.setQuatoCode(wlyyQuota.getId());
            wlyyQuotaResult.setQuatoName(wlyyQuota.getName());
            wlyyQuotaResult.setQuotaDate(getYesterday());
            wlyyQuotaResult.setCreateTime(new Date());
            wlyyQuotaResult.setLevel1Type("3");
            //判断全科医生是否有咨询量
            if (tjTownMap.containsKey(town.getCode())) {
                wlyyQuotaResult.setResult(tjTownMap.get(town.getCode()) + "");
            } else {
                wlyyQuotaResult.setResult("0");
            }
            wlyyQuotaResultDao.save(wlyyQuotaResult);
        }
        //保存市级的统计
        {
            WlyyQuotaResult wlyyQuotaResult = new WlyyQuotaResult();
            wlyyQuotaResult.setDel("1");
            wlyyQuotaResult.setCity(Constant.city);
            wlyyQuotaResult.setCityName(Constant.cityName);
            wlyyQuotaResult.setQuatoCode(wlyyQuota.getId());
            wlyyQuotaResult.setQuatoName(wlyyQuota.getName());
            wlyyQuotaResult.setQuotaDate(getYesterday());
            wlyyQuotaResult.setCreateTime(new Date());
            wlyyQuotaResult.setLevel1Type("4");
            //判断全科医生是否有咨询量
            if (cityCount > 0) {
                wlyyQuotaResult.setResult(cityCount + "");
            } else {
                wlyyQuotaResult.setResult("0");
            }
            wlyyQuotaResultDao.save(wlyyQuotaResult);
        }

        wlyyJobLog.setJobEndTime(new Date());
        wlyyJobLog.setJobContent("统计"+getYesterday()+" 的咨询数据完成 ");
        wlyyJobLog.setJobType("1");
        wlyyJobLogDao.save(wlyyJobLog);
    }
    public String getYesterday() {
        return yesterday;
    }
}
